package com.motorhome.manage.dao.impl;

import com.motorhome.base.dao.Pager;
import com.motorhome.base.dao.impl.BaseDaoImpl;
import com.motorhome.manage.dao.OrderDao;
import com.motorhome.manage.domain.Order;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

/**
 * @author: John
 */
@Repository
public class OrderDaoImpl extends BaseDaoImpl implements OrderDao {

    @Override
    public Pager<Order> getOrderList(int page, int pageSize) {
        return queryForPageByPageNo("select id,user_id,user_phone,user_name,start_city_id,start_city_name," +
                        "start_shop_id,start_shop_name,start_date,end_city_id,end_city_name,end_shop_id," +
                        "end_shop_name,end_date,car_id,car_name,car_description,price,days,total,status,create_time,open_point_no," +
                        "total_snapshot,pay_type,pay_status " +
                        "from uc_order order by create_time desc",
                new Object[]{}, new BeanPropertyRowMapper<>(Order.class), page, pageSize);
    }

    @Override
    public int deleteOrder(long id) {
        return update("delete from uc_order where id = ?", id);
    }

    @Override
    public int updateOrderStatus(long id, int status) {
        return update("update uc_order set status = ? where id = ?", status, id);
    }

    @Override
    public Order getOrderById(long id) {
        return queryForObject("select t1.id,t1.user_id,t1.user_phone,t1.user_name,t1.start_city_id,t1.start_city_name," +
                "t1.start_shop_id,t1.start_shop_name,t1.start_date,t1.end_city_id,t1.end_city_name,t1.end_shop_id," +
                "t1.end_shop_name,t1.end_date,t1.car_id,t1.car_name,t1.car_description,t1.price,t1.days,t1.total,t1.status,t1.create_time," +
                "t2.card_id user_card_id,t1.pay_type,t1.pay_status " +
                "from uc_order t1 left join uc_user t2 on t1.user_id = t2.id " +
                "where t1.id = ?",
                new BeanPropertyRowMapper<>(Order.class), id);
    }

    @Override
    public int updateOrderPoint(String id, String openPointNo) {
        return update("update uc_order set open_point_no = ? where id = ?", openPointNo, id);
    }
}
